package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.service.CateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Copyright (C), 2021-2022
 * Author: 16372
 * Date: 2022/2/24 20:19
 * FileName: CateController
 * Description: 内容管理
 */
@RestController
@RequestMapping("ydd_cate")
public class CateController {

    @Autowired
    private CateService cateService;
    /**
     * @Author 16372
     * @Description //TODO 获取分类详情
     * @Date 20:41 2022/2/24
     * @Param []
     * @return com.itheima.ydd.common.ResultData
     **/
    @GetMapping("/rowInfo")
    public ResultData getRowInfo(String id){
        Cate cate = cateService.getById(id);
        return ResultData.ok("",cate);
    }
    /**
     * @Author 16372
     * @Description //TODO 获取分类列表
     * @Date 11:00 2022/2/25
     * @Param [catename, page, limit] 模糊查询条件 分页条件
     * @return com.itheima.ydd.common.ResultData 结果集封装对象
     **/
    @GetMapping("/list")
    public ResultData getListPage(String catename,
                           Integer page,
                           Integer limit){
        Page<Cate> catePage = new Page<>(page, limit);
        System.out.println("catename = " + catename);
        cateService.page(catePage, new LambdaQueryWrapper<Cate>()
                .like(catename != null && catename != "", Cate::getCatename, catename));
       /* PageResult pageResult = PageResult.init(page1.getTotal()
                , page1.getSize()
                , page1.getCurrent()
                , page1.getRecords());*/
        return ResultData.ok("",PageResult.init(catePage));
    }

    /**
     * @Author 16372
     * @Description //TODO 添加分类
     * @Date 11:24 2022/2/25
     * @Param [cate]    从前端获取分类对象
     * @return com.itheima.ydd.common.ResultData
     **/
    @PostMapping("/add")
    public ResultData toAdd(@RequestBody Cate cate){
        System.out.println("cate = " + cate);
        cateService.save(cate);
        return ResultData.ok("","");
    }

    /**
     * @Author 16372
     * @Description //TODO 根据id删除分类
     * @Date 11:29 2022/2/25
     * @Param [id] 分类主键
     * @return com.itheima.ydd.common.ResultData
     **/
    @GetMapping("/del")
    public ResultData toDelById(Integer id){
        boolean removeById = cateService.removeById(id);
        if (removeById){
            return ResultData.ok("","");
        }
        return ResultData.error("删除失败");
    }

    /**
     * @Author 16372
     * @Description //TODO 分类对象修改
     * @Date 11:35 2022/2/25
     * @Param [cate]
     * @return com.itheima.ydd.common.ResultData
     **/
    @PostMapping("/update")
    public ResultData toUpdate(@RequestBody Cate cate){
        cateService.updateById(cate);
        return ResultData.ok("","");
    }

    @GetMapping("/all")
    public ResultData getAll(){
        List<Cate> list = cateService.list();
        return ResultData.ok("",list);
    }
}
